#!/usr/bin/env python2.5
# encoding: utf-8

class Timeline(Canvas):
  def __init__(self, master, **options):
    self.master      = master
    self.focusTime     = 0
    self.zoom      = 1
    self.activeEvent   = ("", "")
    self.eventTagPattern = re.compile(r'^Event: ([^ ]*) ([^ ]*)$')
    
    Canvas.__init__(self, master, options)
    
    self.bind("<ButtonPress-1>",   self.buttonPress)
  
  def buttonPress(self, event):
    canvasx = self.canvasx(event.x)
    canvasy = self.canvasy(event.y)
    
    self.focusTime = canvasx / self.zoom
    
    found = False
    
    tags = self.gettags(self.find_closest(canvasx, canvasy))
    
    for tag in tags:
      matches = self.eventTagPattern.search(tag)
      
      if matches:
        found = True
        name  = matches.groups()[0]
        id  = matches.groups()[1]
    
    if found:
      self.activeEvent = (name, id)
      print "ActiveEvent: ", self.activeEvent, "; (id, startTime, endTime, value): ", streams[name].getById(id)
    else:
      self.activeEvent = ("", "")
    
    self.render()
  
  def render(self):
    global render
    
    renderStartTime = time.clock()
    render()
    
    print "Rendered in %.2f seconds"%(time.clock() - renderStartTime)
